package cn.iocoder.yudao.module.pypm.controller.admin.iteration;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pypm.controller.admin.iteration.vo.*;
import cn.iocoder.yudao.module.pypm.dal.dataobject.iteration.IterationDO;
import cn.iocoder.yudao.module.pypm.service.iteration.IterationService;

@Tag(name = "管理后台 - 迭代信息")
@RestController
@RequestMapping("/pypm/iteration")
@Validated
public class IterationController {

    @Resource
    private IterationService iterationService;

    @PostMapping("/create")
    @Operation(summary = "创建迭代信息")
    @PreAuthorize("@ss.hasPermission('pypm:iteration:create')")
    public CommonResult<Long> createIteration(@Valid @RequestBody IterationSaveReqVO createReqVO) {
        return success(iterationService.createIteration(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新迭代信息")
    @PreAuthorize("@ss.hasPermission('pypm:iteration:update')")
    public CommonResult<Boolean> updateIteration(@Valid @RequestBody IterationSaveReqVO updateReqVO) {
        iterationService.updateIteration(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除迭代信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pypm:iteration:delete')")
    public CommonResult<Boolean> deleteIteration(@RequestParam("id") Long id) {
        iterationService.deleteIteration(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得迭代信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pypm:iteration:query')")
    public CommonResult<IterationRespVO> getIteration(@RequestParam("id") Long id) {
        IterationDO iteration = iterationService.getIteration(id);
        return success(BeanUtils.toBean(iteration, IterationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得迭代信息分页")
    @PreAuthorize("@ss.hasPermission('pypm:iteration:query')")
    public CommonResult<PageResult<IterationRespVO>> getIterationPage(@Valid IterationPageReqVO pageReqVO) {
        PageResult<IterationDO> pageResult = iterationService.getIterationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IterationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出迭代信息 Excel")
    @PreAuthorize("@ss.hasPermission('pypm:iteration:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIterationExcel(@Valid IterationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IterationDO> list = iterationService.getIterationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "迭代信息.xls", "数据", IterationRespVO.class,
                        BeanUtils.toBean(list, IterationRespVO.class));
    }

}